from mitmproxy import http

#def request(flow: http.HTTPFlow):
#    flow.request.headers["myheader"] = "value"   # 增加一个header名为myheader
#    flow.request.headers["Host"] = "xpchen.com"  # 修改host
#    flow.request.method = "POST"                 # 修改请求访问为POST
#    flow.request.path = "/search?expanded=true"

def response(flow: http.HTTPFlow):
    # request => json
    # filter => jsonpath xpath regex

    # 只有当请求中包含search且参数等于appium时才将appium替换成mock
    if "search" in flow.request.path and flow.request.query.get("q") == "appium" :
        flow.response.text = flow.response.text.replace("appium", "mock")
